package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp02list.ResultList;


public class ResultListWqjImpl implements ResultList {


    private Result[] results;
    private int index;

    public ResultListWqjImpl(int capacity) {
        results = new Result[capacity];
        index = 0;
    }

    @Override
    public void add(Result result) {
        if(index + 1 >= results.length){
            Result[] newResult = new Result[results.length*2];
            System.arraycopy(results, 0, newResult, 0, index + 1);
            results = newResult;
        }
        results[index] = result;
        index++;

    }
    @Override
    public Result get(int index) {
        if (index > size() - 1 || size() == 0){
            throw new IndexOutOfBoundsException("Index: " + index +", Size: " + size());
        }
        return results[index];
    }

    @Override
    public int size() {
        int count = 0;
        for (int i = 0; i < index; i++){
            if (null != results[i]){
                count ++;
            } else {
                break;
            }
        }
        return count;
    }

    @Override
    public Result findByGender(String gender) {
        for (int i = 0; i < index; i++){
            if (get(i).getKey().equals(gender)){
                return get(i);
            }
        }
        return null;
    }

    @Override
    public Result findByDepartment(String department) {
        for (int i = 0; i < index; i++){
            if (get(i).getKey().equals(department)){
                return get(i);
            }
        }return null;
    }

    @Override
    public Result findByJobTitle(String jobTitle) {
        for (int i = 0; i < size(); i++){
            if (get(i).getKey().equals(jobTitle)){
                return get(i);
            }
        }
        return null;
    }

}
